package com.acc.controller;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 
 * @author 杨天然
 *
 */
public class LoginCheckServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginCheckServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
			String userName = request.getParameter("userName");
//			String type = request.getParameter("type");
			if(userName != null) {
				Pattern pattern = Pattern.compile("[a-zA-Z]{1}[a-zA-Z0-9_.]{4,19}");
				Matcher macher = pattern.matcher(userName);
				boolean userName_macher = macher.matches();
				if (!userName_macher) {
					System.out.println(1);
					int status = 200;
					response.setContentType("text/javascript");
					response.getWriter().print(status);
					request.setAttribute("status", status);
				} else {
					int status = 100;
					response.setContentType("text/javascript");
					response.getWriter().print(status);
					request.setAttribute("status", status);
				}
			}
			String password = request.getParameter("password");
			if (password != null) {
				Pattern pattern1 = Pattern.compile("[a-zA-Z0-9]{6,10}");
				Matcher macher1 = pattern1.matcher(password);
				boolean password_macher = macher1.matches();
				if (!password_macher) {
					int status = 200;
					response.setContentType("text/javascript");
					response.getWriter().print(status);
					request.setAttribute("status", status);
				} else {
					int status = 100;
					response.setContentType("text/javascript");
					response.getWriter().print(status);
					request.setAttribute("status", status);
				}
			}
	}

}
